package com.baosight.sczx.zm.cn.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cn.CoalCode;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cn.domain.ZMCNMcmodule;
import com.baosight.sczx.zm.cn.domain.ZMCNYdzhcy;
import com.baosight.sczx.zm.cn.domain.ZMCNYdzhcyMain;
/**
 * 月度综合采样结果管理
 */
public class ServiceZMCN11 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "50");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		
		
		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdForeignId=inInfo.getString("fdForeignId");
//		String fdReportDepartmentId=inInfo.getString("fdReportDepartmentId");
		if(fdForeignId!=null && !fdForeignId.equals("")){
			inInfo.setCell("inqu_status", 0, "fdForeignId", fdForeignId);
		}
//		inInfo.setCell("inqu_status", 0, "fdReportDepartmentId", fdReportDepartmentId);
		EiInfo outInfo = super.query(inInfo, "ZMCNYdzhcy.query", new ZMCNYdzhcy());
		
		outInfo.addBlock(getMines()); 
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[] { "草拟", "上报", "已发布" }, new String[] { "草拟", "上报", "已发布" }, false));// 状态
		String mzCode=InitialData.getInstance().getEiBlockByNo("cmes.cn.meizhiCode").getCellStr(0, "display");
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true,mzCode));
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		return outInfo;
	}	
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCNYdzhcyMain.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}

	/**
	 * 明细新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EIInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-6-23 14:09:37 李红星
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outinfo = new EiInfo();
		ZMCNYdzhcy _dom = new ZMCNYdzhcy();
		HashMap<?,?> eiRow=(HashMap<?,?>)inInfo.getBlock("inqu_status").getRows().get(0);
		String fdForeignId=(String)eiRow.get("fdForeignId");//主表主键
		String fdYear=(String)eiRow.get("fdYear");//主表年份
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_YDZHCY"));
			_dom.setFdForeignId(fdForeignId);
			_dom.setFdYear(fdYear);
			this.dao.insert("ZMCNYdzhcy.insert", _dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}

	 
	/**
	 * 明细修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-06-24 10:42:11 李红星
	 * </p>
	 *
	 */
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCNYdzhcy _dom = new ZMCNYdzhcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
		    this.dao.update("ZMCNYdzhcy.update", _dom);			
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNYdzhcy _dom = new ZMCNYdzhcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCNYdzhcy.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}
	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	public EiInfo report(EiInfo inInfo) {
		super.update(inInfo, "ZMCNYdzhcy.report");
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 校验明细是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-6-27 14:21:21 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNYdzhcy _dom = new ZMCNYdzhcy();
		HashMap<?,?> eiRow=(HashMap<?,?>)inInfo.getBlock("inqu_status").getRows().get(0);
		String fdForeignId=(String)eiRow.get("fdForeignId");//主表主键
		String fdYear=(String)eiRow.get("fdYear");//主表年份
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String fdMonth = _dom.getFdMonth();
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdForeignId", fdForeignId);
			params.put("fdMonth", fdMonth);
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			List result = dao.query("ZMCNYdzhcy.validate", params);
	//		EiInfo out=new EiInfo();
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCNYdzhcy _temp = (ZMCNYdzhcy)result.get(0);
			
				StringBuffer msg = new StringBuffer("提交失败，");
				if(_temp.getFdMonth() != null && _temp.getFdMonth().equals(fdMonth)) {
					msg.append(" 月份 ");
				}
	
				msg.append("重复！");
				
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		return inInfo;
	}
	
	/**
	 * 校验明细是否可修改或删除
	 * <p>Function: validate</p>
	 * <p>Description:当明细表数据生效时，不能修改或删除 </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-15 22:06:05 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate1(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		ZMCNYdzhcyMain _dom = new ZMCNYdzhcyMain();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdKeyId", _dom.getFdKeyId());
			List result = dao.query("ZMCNYdzhcy.inteface_vali", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer("提交失败，");
				msg.append("明细已经上报，不能删除或修改！");
				
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
} 